<?php

namespace app\common\model;

use think\Model;


class Member extends Model
{

    

    

    // 表名
    protected $table = 'member';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'categories_text'
    ];



    public function getCategoriesTextAttr($value, $data)
    {
        $groupNames = (new \app\common\model\MemberCategory())->whereIn('id',$data['member_category_ids'])->select();

        $names = [];
        foreach ($groupNames as $key => &$row) {
            $pcategory = (new \app\common\model\MemberCategory())->find($row['pid']);
            $row['name'] = $pcategory ? $pcategory['name'].'：'.$row['name'] : $row['name'];
            $names[] = $row['name'];
        }
        return implode('，',$names);
    }






    public function category()
    {
        return $this->belongsTo('app\common\model\MemberCategory', 'member_category_ids', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
